<?php
/*
Plugin Name: HelloTxt post
Plugin URI: http://hellotxt.com/app/wp-plugin/hellotxt.zip
Description: Auto post your blogs on HelloTxt.
Version: 1.0
Author: Fabrizio Giordano
Author URI: http://fabriziogiordano.com
*/

function hellotxtpost_install() {
	global $wpdb;
	$table_name = $wpdb->prefix."hellotxt";
	if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
		$sql = "CREATE TABLE " . $table_name . " (id mediumint(9) NOT NULL AUTO_INCREMENT, hellotxt_user_key VARCHAR(100) NOT NULL, hellotxt_group VARCHAR(20) NOT NULL, );";
	}
	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	dbDelta($sql);
}

function send_to_hellotxt($post_ID) {
	$app_key = '1GNntqlC6X7UtVwj';
	$user_key = get_option('hellotxt_user_key');
	$group = get_option('hellotxt_group');
	$posted = get_post($post_ID);
	$ch = curl_init();    // initialize curl handle
	curl_setopt($ch, CURLOPT_URL,"http://hellotxt.com/api/v1/method/user.post"); // set url to post to
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
	curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s
	curl_setopt($ch, CURLOPT_POST, 1); // set POST method
	curl_setopt($ch, CURLOPT_POSTFIELDS, 
		"app_key=".$app_key.
		"&user_key=".$user_key.
		"&body=".$posted->post_title.' - '.$posted->guid.
		"&group=".$group.
		"&"); // add POST fields
	$result = curl_exec($ch); // run the whole process
	curl_close($ch);  
}
function hellotxtpost_menu() {
  add_options_page('HelloTxt Post Options', 'HelloTxt Post', 8, __FILE__, 'hellotxtpost_options');
}

function hellotxtpost_options() {
echo '<div class="wrap">';
echo '<h2>HelloTxt post</h2>';

echo '<form method="post" action="options.php">';
echo wp_nonce_field('update-options');

echo '<table class="form-table">';

echo '<tr valign="top">';
echo '<th scope="row">HelloTxt <a href="http://hellotxt.com/settings/api/wordpress-plug-in">User Key</a></th>';
echo '<td><input type="text" name="hellotxt_user_key" value="'.get_option('hellotxt_user_key').'" /></td>';
echo '</tr>';
 
echo '<tr valign="top">';
echo '<th scope="row">Group</th>';
echo '<td><select name="hellotxt_group">
		<option value="inhome">Inhome</option>
		<option value="friend">Friend</option>
		<option value="colleague">Colleague</option>
	  </td>';
echo '</tr>';

echo '</table>';

echo '<input type="hidden" name="action" value="update" />';
echo '<input type="hidden" name="page_options" value="hellotxt_user_key,hellotxt_group" />';

echo '<p class="submit">';
echo '<input type="submit" name="Submit" value="'._e('Save Changes').'" />';
echo '</p>';

echo '</form>';
echo '</div>';

}

// Create the tables
// register_activation_hook(__FILE__,'hellotxtpost_install');
// Create the admin menu
add_action('admin_menu', 'hellotxtpost_menu');
// Create the publish post action
add_action('publish_post', 'send_to_hellotxt');
?>
